package app;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Holder for words that is used by WordsFrequencyCalculator
 */

public class WordStatisticHolder implements Comparable <WordStatisticHolder> {
    String word;
    int word_frequence;
    int total_words_amount;
    String separator;

    public WordStatisticHolder (String w, ArrayList<String> w_f, String s) {
        word = w;
        word_frequence = Collections.frequency(w_f, word); //don't like it
        total_words_amount = w_f.size();
        separator = s;
    }

    public boolean equals (Object word){
        WordStatisticHolder w = (WordStatisticHolder) word;
        return getWord().equals(w.getWord());
    }

    public int hashCode() {
        return word.hashCode();
    }

    public int compareTo (WordStatisticHolder wh) {
        return Integer.compare(word_frequence, wh.getFrequence());
    }

    public int getFrequence () {
        return word_frequence;
    }

    public String getWord () {
        return word;
    }

    public void setWord (String word) {
        this.word = word;
    }

    public void setWord_frequence (int frequence){
        this.word_frequence=frequence;
    }

    public String toString() {
        return word + separator + word_frequence + separator +
                new DecimalFormat("##.##").format(100.0 * word_frequence / total_words_amount) + "%";
    }
}
